package com.baizhi.service.impl;

import com.baizhi.entity.CmfzGuru;
import com.baizhi.mapper.CmfzGuruMapper;
import com.baizhi.service.CmfzGuruService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.Scanner;

/**
 * <p>
 * InnoDB free: 11264 kB 服务实现类
 * </p>
 *
 * @author xiaoxiao
 * @since 2020-08-18
 */
@Transactional
@Service
public class CmfzGuruServiceImpl extends ServiceImpl<CmfzGuruMapper, CmfzGuru> implements CmfzGuruService {

    @Override
    public void writeExcel() {
        //准备数据
        String[] titles = {"上师编号","上师姓名","上师图片地址","上师法号","上师状态"};
        List<CmfzGuru> guruList = baseMapper.selectList(null);
        //new 一个excel对象
        HSSFWorkbook workbook = new HSSFWorkbook();
        //根据Excel对象 获取表
        HSSFSheet sheet = workbook.createSheet("xiaoxiao");
        //根据表 获取列
        HSSFRow row = sheet.createRow(0);
        for (int i = 0; i < titles.length; i++) {
            //根据列 获取行
            HSSFCell cell = row.createCell(i);
            cell.setCellValue(titles[i]);
        }
        //为表中添加数据
        for (int i = 0; i < guruList.size(); i++) {
            row = sheet.createRow(i+1);
            row.createCell(0).setCellValue(guruList.get(i).getGuruId());
            row.createCell(1).setCellValue(guruList.get(i).getGuruName());
            row.createCell(2).setCellValue(guruList.get(i).getGuruImage());
            row.createCell(3).setCellValue(guruList.get(i).getGuruNickname());
            row.createCell(4).setCellValue(guruList.get(i).getGuruStatus());
        }
        //保存到本地磁盘中
        try {
            workbook.write(new FileOutputStream("D://Excel/xiaoxiao.xls"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    @Transactional(readOnly = true)
    public PageInfo<CmfzGuru> selectAllByPage(Integer page, Integer limit) {
        PageHelper.startPage(page,limit);
        List<CmfzGuru> guruList = baseMapper.selectList(null);
        return new PageInfo<>(guruList);
    }

    @Override
    @Transactional(readOnly = true)
    public CmfzGuru selectById(Integer id) {
        CmfzGuru cmfzGuru = baseMapper.selectById(id);
        return cmfzGuru;
    }
}
